'''
Created on 24-05-2012

@author: ahaw
'''

from PyQt4 import QtGui,QtCore
from Settings import *
from form.DialogForm import *
class OptionsForm(QtGui.QDialog):

    def __init__(self, parent=None):
        
        '''
        Gui okna preferencji
        laczenie ze zdarzeniami
        '''
        super(OptionsForm, self).__init__(parent)

        label2 = QtGui.QLabel("Katalogi biblioteki")
        
        addDirectoryButton = QtGui.QPushButton("Dodaj folder",self)
        addDirectoryButton.setToolTip('Tutaj <b>dodajemy</b> katalogi do biblioteki')
        addDirectoryButton.clicked.connect(self.addFolderToLibraryList)
        rmDirectoryButton= QtGui.QPushButton("Usun folder", self)
        rmDirectoryButton.clicked.connect(self.rmFolderFromLibraryList)
        pathsLayout = QtGui.QGridLayout()
        pathsLayout.addWidget(label2)
        toolPathGroupBox = QtGui.QGroupBox("Opcje")
        toolPathGroupBox.setLayout(pathsLayout)
        layout = QtGui.QVBoxLayout()
        self.libraryDirectoriesListWidget=libraryListWidget()
         

        layout.addWidget(toolPathGroupBox)
        layout.addWidget(self.libraryDirectoriesListWidget)
        buttonLayout = QtGui.QHBoxLayout()
        buttonLayout.addWidget(addDirectoryButton)
        buttonLayout.addWidget(rmDirectoryButton)
        
        layout.addLayout(buttonLayout)
        #layout.addWidget(self.pyuic4xCheckBox)
        #layout.addWidget(buttonBox)
        self.setLayout(layout)
        self.setWindowTitle("PyQt - Options")
        
    def reloadDirectories(self):
        '''
        Odswierzanie listy folderow biblioteki
        '''
        self.libraryDirectoriesListWidget.clear()
        for i in Settings.instance.libraryDirectories:
            self.libraryDirectoriesListWidget.addItem(i)
    def addFolderToLibraryList(self):
        '''
        Dodawanie folderu do listy folderow.
        '''
        folder=OpenFolderDialog()
        folder.setFileMode(QtGui.QFileDialog.DirectoryOnly)
        if folder.exec_() == 1:
            directory = folder.selectedFiles()[0]
            if (directory[-1]!="/"):
                directory+="/"
            Settings.addDirectory(Settings.instance,directory)
            self.reloadDirectories()    
    def rmFolderFromLibraryList(self):
        '''Make
        Usuwanie folderu z listy folderow biblioteki
        '''
        print (self.libraryDirectoriesListWidget.currentItem().text())
        Settings.instance.libraryDirectories.remove(self.libraryDirectoriesListWidget.currentItem().text())
        Settings.instance.saveSettings()
        self.reloadDirectories()

class libraryListWidget(QtGui.QListWidget):

    def __init__(self):
        '''
        Konstruowanie ListWidget z lista folderow biblioteki
        '''
        super(libraryListWidget, self).__init__()
        print (Settings.instance.libraryDirectories)
        for each in Settings.instance.libraryDirectories:
            self.addItem(each)

def main():   
    app = QtGui.QApplication(sys.argv)
    app.setApplicationName("PyMp - Python Music Player")
    app.setOrganizationName('PyMP')
    Settings()   
    form= OptionsForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()